import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

import Login from "@/views/login.vue";
import Home from "@/views/Home.vue";
import Zhuce from "@/views/zhuce.vue";
import Myto from "@/views/myto";
import Fenlei from "@/views/fenlei";
import Shoppingcart from "@/views/shoppingcart";
import goodsDetail from "@/views/goodsDetail";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'homeView',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },

  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/myto',
    name: 'myto',
    component: Myto,
    meta:{

      isLogin:true
    }
  },

  {
    path: '/zhuce',
    name: 'zhuce',
    component: Zhuce
  },

  {
    path: '/fenlei',
    name: 'fenlei',
    component: Fenlei
  },
  {
    path:'/shoppingcart',
    name:'shoppingcart',
    component: Shoppingcart
  },
  {
    path: "/detail/:id",
    name: "detail",
    component: goodsDetail
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes
})
//from 原界面  /#/home
//to 目标界面
//next 是一个函数 调用这个函数 就可以跳转到目标界面
export default router
//路由守卫 前置守卫
router.beforeEach((to,from,next) =>{

  console.log("这里是路由守卫")

  console.log(to)

  console.log(from)

  //获取目标界面的额外信息 这个信息包含目标界面需不需要登录
  var islogin = to.meta.isLogin
  var logined = sessionStorage.getItem("logined")
  //如果目标界面需要登录 并且登录操作是否完成
  if(islogin == true && !logined ) {
    //如果目标界面需要登录，则跳转到登录页面
    next("/login")

  }else {
    //如果无需要登录，则跳转到目标界面
    //调用这个函数就可以从界面跳转到目标界面
    next()
  }
})
